package com.woniuxy.dr_mall.controller;


import com.woniuxy.dr_mall.entity.*;
import com.woniuxy.dr_mall.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.web.session.HttpServletSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张哲轩
 * @since 2021-08-11
 */
@Controller
@Slf4j
@RequestMapping("/dr_mall/drCart")
public class DrCartController {

    @Autowired
    private IDrCartService cartService;
    @Autowired
    private IDrSpecValService specValService;

    @Autowired
    private IDrUserService userService;

    @Autowired
    private IDrOrdersService ordersService;

    @Autowired
    private IDrSpecPriceService specPriceService;
    @Autowired
    private IDrOrdersItemService ordersItemService;

    @PostMapping("joinCart")
    @ResponseBody
    public MyResponseEntity joinCart(Integer skuId,HttpSession session) {
        DrUser user = (DrUser) session.getAttribute("user");
        DrCart cart = new DrCart();
        cart.setChecked("y");
        cart.setUserId(user.getId());
        cart.setSkuId(skuId);
        cart.setPrice(specPriceService.getById(skuId).getPrice());
        cartService.save(cart);
        return MyResponseEntity.SUCCESS;
    }

    @GetMapping("cartList")
    public String cartList(Model model,HttpSession session) {
        DrUser user=(DrUser) session.getAttribute("user");
        List<DrCart> carts = cartService.getCartsByUserId(user.getId());
        for (DrCart cart : carts) {
            StringBuilder specTotalVal = new StringBuilder("");
            DrSpecPrice specPrice = cart.getSkus();
            //拿购物车里商品的图片信息
            DrPicture picture = cart.getSkus().getProducts().getPicture();
            //从购物车里拿商品
            DrProducts product = cart.getSkus().getProducts();
            //将图片信息加到商品里面.
//            product.setPicture(pictures.get(0));
            specPrice.setProducts(product);
            if (specPrice.getMaterial() != null) {
                DrSpecVal specVal = specValService.getById(specPrice.getMaterial());
                specTotalVal.append(specVal.getSpecVal() + " ");
            }
            if (specPrice.getWeight() != null) {
                DrSpecVal specVal = specValService.getById(specPrice.getWeight());
                specTotalVal.append(specVal.getSpecVal()).append(" ");
            }
            if (specPrice.getColor() != null) {
                DrSpecVal specVal = specValService.getById(specPrice.getColor());
                specTotalVal.append(specVal.getSpecVal()).append(" ");
            }
            if (specPrice.getSeries() != null) {
                DrSpecVal specVal = specValService.getById(specPrice.getSeries());
                specTotalVal.append(specVal.getSpecVal()).append(" ");
            }
            if (specPrice.getSize() != null) {
                DrSpecVal specVal = specValService.getById(specPrice.getSize());
                specTotalVal.append(specVal.getSpecVal()).append(" ");
            }
            if (specPrice.getType() != null) {
                DrSpecVal specVal = specValService.getById(specPrice.getType());
                specTotalVal.append(specVal.getSpecVal()).append(" ");
            }
            specPrice.setSpecVal(specTotalVal.toString());
            cart.setSkus(specPrice);
        }
        log.debug("carts:{}", carts.toString());
        model.addAttribute("carts", carts);
        return "cart";
    }

    @GetMapping("clearCart")
    public String clearCart(HttpSession session) {
        DrUser user= (DrUser) session.getAttribute("user");
        cartService.clearCart(user.getId());
//        cartService.clearCart(1);
        return "redirect:/dr_mall/drCart/cartList";
    }

    @GetMapping("del/{id}")
    public String del(@PathVariable int id) {
        cartService.removeById(id);
        return "redirect:/dr_mall/drCart/cartList";
    }

    @PostMapping("loadCartAgreement")
    public String loadCartAgreement(Integer[] cartId, Model model, Integer skuId, HttpSession session) {
        log.debug("cart:{}", cartId);
        DrUser user = (DrUser) session.getAttribute("user");
        model.addAttribute("cartId", cartId);
        model.addAttribute("skuId", skuId);
        return "cart_agreement";
    }

    /**
     * 检查下单过的物品
     *
     * @return json 数据
     */
    @GetMapping("vpe")
    @ResponseBody
    public MyResponseEntity vpe(HttpSession session) {
        DrUser user= (DrUser) session.getAttribute("user");
        List<DrProducts> products = ordersService.getOrdersByUserId(user.getId());
        log.debug("products:{}", products);
        return new MyResponseEntity(products);
    }
}

